#include <bits/stdc++.h>

using namespace std;
const int maxn = 2e5 + 15;
int u, v, p, w, n, q, cnt, head[maxn];
struct edge {
    int to, nxt, w;
}e[maxn << 1];
inline void add(int u, int v, int w) {
    e[++cnt] = (edge){ v,head[u],w };
    head[u] = cnt;
}
int dp[maxn][20], f[maxn], d[maxn], g[maxn][20];
void dfs(int x, int fa, int depth) {
    dp[x][0] = fa; f[x] = fa; d[x] = depth;
    for (int i = head[x];i > 0;i = e[i].nxt) {
        int v = e[i].to;
        if (v == fa) continue;
        dfs(v, x, depth + 1); g[v][0] = e[i].w;
    }
}
void init() {
    for (int i = 1;(1 << i) + 1 <= n; ++i) {
        for (int j = 1;j <= n; ++j) {
            dp[j][i] = dp[dp[j][i - 1]][i - 1];
            g[j][i] = max(g[j][i - 1], g[dp[j][i - 1]][i - 1]);
        }
    }
}
int query(int u, int v) {
    int ans = 0x3f3f3f3f;
    if (d[u] < d[v]) swap(u, v);
    int h = d[u] - d[v];
    for (int i = 0;i < 20; ++i) {
        if (h & (1 << i)) {
            ans = min(ans, g[u][i]);
            u = dp[u][i];
        }
    }
    if (u == v) return ans;
    for (int i = 19; ~i; --i) {
        if (dp[u][i] != dp[v][i]) {
            ans = max(max(g[u][i], g[v][i]), ans);
            u = dp[u][i]; v = dp[v][i];
        }
    }
    return max(ans, max(g[u][0], g[v][0])); //最后跳到的是LCA的儿子节点,所以还要求最大值
}
int main() {
    scanf("%d %d", &n, &q);
    for (int i = 1;i <= n; ++i) {
        scanf("%d %d", &p, &w);
        if (p == 0) continue;
        add(i, p, w); add(p, i, w);
    }
    dfs(1, 0, 1); init();
    while (q--) {
        scanf("%d %d", &u, &v);
        printf("%d\n", query(u, v));
    }
    return 0;
}